package org.springblade.mes.system.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.mes.system.entity.Material;
import org.springblade.mes.system.mapper.MaterialMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 生产物料表 服务实现类
 *
 * @author 翁金川
 * @since 2025-06-24
 */
@Service
@DS("system")
public class MaterialService extends BaseServiceImpl<MaterialMapper, Material> {
    /**
     * 根据编码查询
     *
     * @param no
     * @param tenantId
     * @param ctx
     * @return
     */
    public Material findByNo(String no, String tenantId, Map<String, Object> ctx) {
        String key = "Mat_" + no;
        Object v = ctx.get(key);
        if (v instanceof Material) {
            return (Material) v;
        }
        LambdaQueryWrapper<Material> lambdaQueryWrapper = Wrappers.lambdaQuery(Material.class);
        lambdaQueryWrapper.eq(Material::getMaterialNo, no).eq(Material::getTenantId, tenantId);
        List<Material> dataList = this.list(lambdaQueryWrapper);
        if (dataList == null || dataList.size() == 0) {
            return null;
        }
        Material t = dataList.get(0);
        ctx.put(key, t);
        return t;
    }
}
